<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="宿舍栋号" prop="buildingNo">
        <el-select
          v-model="queryParams.buildingNo"
          placeholder="宿舍栋号"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in buildingNoOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="楼层" prop="storey">
        <el-select
          v-model="queryParams.storey"
          placeholder="宿舍楼层"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in storeyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="宿舍号" prop="dormitoryNo">
        <el-input
          v-model="queryParams.dormitoryNo"
          placeholder="请输入宿舍号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="宿舍状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basedata:bdormitory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basedata:bdormitory:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basedata:bdormitory:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bdormitoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="宿舍栋号" align="center" prop="buildingNo" :formatter="buildingNoFormat" />
      <el-table-column label="楼层" align="center" prop="storey" :formatter="storeyFormat" />
      <el-table-column label="宿舍号" align="center" prop="dormitoryNo" :show-overflow-tooltip="true" />
      <el-table-column label="准住人数" align="center" prop="stuCount" :formatter="stuCountFormat" />
      <el-table-column
        label="宿管老师"
        align="center"
        prop="dormitoryTeacherId"
        :show-overflow-tooltip="true"
        :formatter="dormitoryTeacherIdFormat"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            circle
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basedata:bdormitory:edit']"
          />
          <el-button
            size="mini"
            circle
            type="waring"
            icon="el-icon-refresh-right"
            @click="emptyHandle(scope.row)"
            v-hasPermi="['basedata:bdormitory:remove']"
          />
          <el-button
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basedata:bdormitory:remove']"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px">
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-form-item label="宿舍栋号" prop="buildingNo">
              <el-select
                v-model="form.buildingNo"
                placeholder="宿舍栋号"
                clearable
                :disabled="disabledInput"
              >
                <el-option
                  v-for="dict in buildingNoOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-form-item label="楼层" prop="storey">
              <el-select v-model="form.storey" placeholder="楼层">
                <el-option
                  v-for="dict in storeyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-form-item label="宿舍号" prop="dormitoryNo">
              <el-input v-model="form.dormitoryNo" placeholder="请输入宿舍号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-form-item label="宿舍管理员" prop="dormitoryTeacherId">
              <el-select v-model="form.dormitoryTeacherId" filterable placeholder="请选择宿舍管理员">
                <el-option
                  v-for="user in userOptions"
                  :key="user.id"
                  :label="user.username"
                  :value="user.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-form-item label="准住人数" prop="stuCount">
              <el-select @change="checkPerson" v-model="form.stuCount" placeholder="准住人数">
                <el-option
                  v-for="dict in stuCountOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="床位编号" v-if="form.stuCount">
          <div v-for="(item,index) in bedList" :key="index">
            <el-input-number size="mini" v-model="item.bedNo" :min="0" :max="20" style="width:30%"></el-input-number>
            <el-select size="mini" v-model="item.bedType" style="width:30%">
              <el-option value="1" label="上铺"></el-option>
              <el-option value="2" label="下铺"></el-option>
              <el-option value="3" label="上铺下桌"></el-option>
            </el-select>
            <el-tag v-if="item.studentId" type="success">已使用</el-tag>
            <el-tag v-else type="info">未使用</el-tag>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBDormitory,
  getBDormitory,
  addBDormitory,
  updateBDormitory,
  delBDormitory,
  exportBDormitory,
  changeBDormitoryStatus,
  emptyDormitoryBed
} from "@/api/basedata/bdormitory";
import { getDorTeacherList } from "@/api/system/user";

export default {
  name: "BDormitory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 字典表格数据
      bdormitoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 宿舍栋号
      buildingNoOptions: [],
      // 楼层
      storeyOptions: [],
      // 状态数据字典
      statusOptions: [],
      //准住人数
      stuCountOptions: [],
      //宿管老师
      userOptions: [],
      disabledInput: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingNo: undefined,
        storey: undefined,
        dormitoryNo: undefined,
        status: undefined
      },

      // 表单参数
      form: {},
      bedList: [],
      // 表单校验
      rules: {
        buildingNo: [
          { required: true, message: "宿舍栋号不能为空", trigger: "blur" }
        ],
        storey: [{ required: true, message: "楼层不能为空", trigger: "blur" }],
        dormitoryNo: [
          { required: true, message: "宿舍号不能为空", trigger: "blur" }
        ],
        dormitoryTeacherId: [
          { required: true, message: "宿管老师不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDorTeacherList();
    this.getDicts("sys_building").then(response => {
      this.buildingNoOptions = response.data;
    });
    this.getDicts("sys_storey").then(response => {
      this.storeyOptions = response.data;
    });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("stu_count").then(response => {
      this.stuCountOptions = response.data;
    });
  },
  methods: {
    // 清空
    emptyHandle(row) {
      console.log(row);
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认清空宿舍编号为"' + ids + '"床位的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          return emptyDormitoryBed(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("清空成功");
        })
        .catch(function() {});
    },
    // 选中人数
    checkPerson(e) {
      this.bedList = [];
      this.selectDictLabel(this.stuCountOptions, e);
      for (let i = 1; i <= this.selectDictLabel(this.stuCountOptions, e); i++) {
        this.bedList.push({
          studentId: undefined,
          bedNo: i,
          bedType: "3"
        });
      }
      console.log(this.form.bedList);
    },
    /** 查询宿舍列表 */
    getList() {
      this.loading = true;
      listBDormitory(this.queryParams).then(response => {
        this.bdormitoryList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getDorTeacherList() {
      getDorTeacherList().then(response => {
        this.userOptions = response.data;
      });
    },
    // 字典设备状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    buildingNoFormat(row, column) {
      return this.selectDictLabel(this.buildingNoOptions, row.buildingNo);
    },
    storeyFormat(row, column) {
      return this.selectDictLabel(this.storeyOptions, row.storey);
    },
    stuCountFormat(row, column) {
      return this.selectDictLabel(this.stuCountOptions, row.stuCount);
    },
    dormitoryTeacherIdFormat(row, column) {
      return this.getLable(
        this.userOptions,
        row.dormitoryTeacherId,
        "id",
        "username"
      );
    },
    /**
     * 根据接收到值显示对应的中文name
     * list：数据源
     * id：传入的值
     * value : list中id对应的key名称
     * lable: list中value对应的key名称
     */
    getLable(list, id, value, lable) {
      if (id != "" && Array.isArray(list) && list.length != 0) {
        return !list.find(item => item[value] == id)
          ? id
          : list.find(item => item[value] == id)[lable];
      } else {
        return id;
      }
    },
    // 宿舍状态修改
    handleStatusChange(row) {
      let text = row.status === "1" ? "启用" : "停用";
      this.$confirm(
        '确认要"' + text + '"序号为"' + row.id + '"的宿舍?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return changeBDormitoryStatus(row.id, row.status);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function() {
          row.status = row.status === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.bedList = [];
      this.form.bedList - [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        buildingNo: undefined,
        storey: undefined,
        dormitoryNo: undefined,
        dormitoryTeacherId: undefined,
        status: "1",
        stuCount: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加宿舍信息";
      this.disabledInput = false;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(999);
      this.reset();
      const id = row.id || this.ids;
      getBDormitory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改宿舍信息";
        //修改时栋号、楼层、宿舍号都不允许修改
        this.disabledInput = true;
        this.bedList = response.data.bedList;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.bedList = this.bedList;
      console.log(this.form);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateBDormitory(this.form.id, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.bedList = [];
                this.form.bedList = [];
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            addBDormitory(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
                this.bedList = [];
                this.form.bedList = [];
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除宿舍编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delBDormitory(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有宿舍数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportBDormitory(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
